/*
 * HalloweenController.c
 *
 * Created: 30/06/2014 7:23:45 PM
 *  Author: MacGyver
 */ 

#include "CDef.hpp"
#include "BInit_328P.hpp"

#ifdef __cplusplus
extern "C"
{
	#endif
		#include <avr/io.h>
		#include <stdbool.h>
		#include <avr/interrupt.h>
		#include <util/delay.h>
	#ifdef __cplusplus
}
#endif
#include "Ports.hpp"



int main(void)
{
	BINI_vInitPorts();

	PORT * mPORTB = new PORT(&PORTB);
	PORT * mPORTD = new PORT(&PORTD);

	/* Configure the PWM channel */
	PWM * mPWM = new PWM(mPORTD->PINs[PORTD6], PWM::MODE_FAST_PWM);
	
	mPWM->vSetPreScaler(PWM::PSCL_DIV_64);
	mPWM->vSetInversion(PWM::INV_NON_INVERTED);
	mPWM->vSetDuty(0);
	
	/* Enable global interrupts */
	sei();
	
	uint8_t u8PWM = 0;
	bool boTog = false;
    while(1)
    {
		_delay_ms(2);
		//mPORTB->PINs[PORTB5]->vClear();
		//_delay_ms(50);
		//mPORTB->PINs[PORTB5]->vSet();
		
		mPWM->vSetDuty(u8PWM);
		
		if(   ( (u8PWM == 0) && boTog) 
		   || ( (u8PWM == 0xFF) && !boTog))
			boTog = !boTog;
		
		if(boTog)
			u8PWM--;
		else
			u8PWM++;			
		
		//if( mPORTB->PINs[PORTB7]->enGet() == PIN::ON )
			//mPORTB->PINs[PORTB5]->vClear();
		//else
			//mPORTB->PINs[PORTB5]->vSet();
    }
	
	delete mPORTB;
}